<template>
  <el-dialog :visible.sync="lastShow" :before-close="close" width='500px' :title="$t('common.base.toast')">
    <p>{{$t('product.qrcodeSetting.lastDialog.text1')}}</p>
    <el-form label-position="top" :inline='true' ref="form" :model='form' :rules='rules'>
      <el-form-item :label="$t('common.paas.view.designer.rule.mobilePhoneNum')" prop="phone">
        <el-input class="inputClz" v-model="form.phone" :placeholder="$t('task.tip.serviceReportTip4')"></el-input>
      </el-form-item>
      <br>
      <el-form-item :label="$t('common.base.weChatAccount')">
        <el-input class="inputClz" v-model="form.wxAccount" :placeholder="$t('product.qrcodeSetting.lastDialog.text2')"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">{{$t('common.base.cancel')}}</el-button>
      <el-button type="primary" :loading='pending' @click="submit">{{$t('common.base.makeSure')}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  insertBaseSupercodeCustomerProvider
} from '@src/api/SuperQrcode';
import { validatePhone as validatePhone_ } from '@src/util/validator'
const validatePhone = (rule,value,cb)=>{
  if(!validatePhone_(value, 1)){
    cb(new Error(this.$t('product.qrcodeSetting.lastDialog.text3')));
  }else{
    cb();
  }
}

export default {
  name:'last-dialog',
  data(){
    return{
      form:{},
      rules:{
        phone:[
          {required:true,message:this.$t('task.tip.serviceReportTip4'),trigger:'blur'},
          {validator:validatePhone,trigger:'blur'}
        ]
      },
      pending:false
    }
  },
  props:{
    lastShow:{
      type:Boolean,
      default:false
    },
    lastForm:{
      type:Object,
      default:()=>{}
    },
    tenantId:{
      type:String,
      default:''
    }
  },
  watch:{
    lastShow(val){
      if(val){
        this.form=Object.assign({},this.lastForm);
      }
    }
  },
  methods:{
    close(bool=false,form){
      this.$emit('close',bool,form);
    },
    submit(){
      this.$refs.form.validate((valid) => {
        if(valid){
          this.pending=true;
          const params={
            ...this.form,
            tenantId:this.tenantId
          }
          insertBaseSupercodeCustomerProvider(params).then(res=>{
            this.pending=false;
            if(res.code==='200'){
              this.close(true,this.form);
            }else{
              this.$platform.notification({
                type: 'error',
                title: this.$t('common.base.toast'),
                message: res.msg
              })
            }
          });
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.inputClz{
  width: 250px;
}
</style>